package c.j.a.d.b.n;

import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Looper;
import android.os.Process;
import android.os.StatFs;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import c.j.a.c.y.a.i;
import c.j.a.d.b.b.g;
import c.j.a.d.b.d.l;
import c.j.a.d.b.e.b;
import c.j.a.d.b.h.d;
import c.j.a.d.b.i.f;
import c.j.a.d.b.j.q;
import com.huawei.hms.support.api.entity.core.CommonCode.StatusCode;
import com.pgl.sys.ces.out.ISdkLite;
import com.ss.android.socialbase.downloader.g.c;
import com.ss.android.socialbase.downloader.g.e;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONObject;

/* compiled from: DownloadUtils */
public class a {
    public static final String a = "a";
    public static String b = null;
    public static volatile SparseArray<Boolean> c = new SparseArray();
    public static volatile SparseArray<List<l>> d = new SparseArray();
    public static final char[] e = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    public static Pattern f = null;
    public static Pattern g = null;
    public static Boolean h;
    public static Boolean i;

    static {
        Pattern.compile(".*\\d+ *- *(\\d+) */ *\\d+");
    }

    public static String A(f fVar, String str) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        String a = fVar.a(str);
        if (!c.j.a.d.b.l.a.f.k("fix_get_http_resp_head_ignore_case", true)) {
            return a;
        }
        if (TextUtils.isEmpty(a)) {
            a = fVar.a(str.toLowerCase());
        }
        if (TextUtils.isEmpty(a)) {
            a = fVar.a(str.toUpperCase());
        }
        return a;
    }

    public static String B(Throwable th, String str) {
        if (str == null) {
            return Y(th);
        }
        StringBuilder i = c.b.a.a.a.i(str, "-");
        i.append(Y(th));
        return i.toString();
    }

    public static void C(c cVar) {
        if (cVar != null) {
            c.j.a.d.b.l.a d = c.j.a.d.b.l.a.d(cVar.g0());
            String str = "anti_hijack_dir";
            JSONObject l = d.l(str);
            if (l != null) {
                CharSequence optString;
                String optString2 = l.optString("ins_desc");
                if (!TextUtils.isEmpty(optString2)) {
                    L(cVar.e, optString2);
                }
                optString2 = cVar.j0();
                if (TextUtils.isEmpty(optString2)) {
                    optString2 = cVar.b;
                }
                JSONObject l2 = d.l(str);
                if (l2 != null) {
                    optString = l2.optString("dir_name");
                    if (!TextUtils.isEmpty(optString) && optString.startsWith("/")) {
                        optString = optString.substring(1);
                    }
                    if (!TextUtils.isEmpty(optString)) {
                        if (optString.contains("%s")) {
                            try {
                                optString = String.format(optString, new Object[]{optString2});
                            } catch (Throwable unused) {
                            }
                        } else {
                            optString = c.b.a.a.a.D(optString, optString2);
                        }
                        if (optString.length() > ISdkLite.REGION_UNSET) {
                            optString = optString.substring(optString.length() - ISdkLite.REGION_UNSET);
                        }
                    }
                } else {
                    optString = "";
                }
                String str2 = cVar.e;
                if (!(TextUtils.isEmpty(optString) || TextUtils.isEmpty(str2))) {
                    File file = new File(optString);
                    File file2 = new File(str2);
                    while (file != null && file2 != null && file2.isDirectory() && TextUtils.equals(file.getName(), file2.getName())) {
                        str2 = file2.getPath();
                        optString2 = a;
                        StringBuilder g = c.b.a.a.a.g("deleteDirIfEmpty on thread: ");
                        g.append(Thread.currentThread());
                        Log.w(optString2, g.toString());
                        if (!TextUtils.isEmpty(str2)) {
                            File file3 = new File(str2);
                            if (file3.isDirectory() && !file3.delete()) {
                                Log.w(optString2, "deleteDirIfEmpty return false");
                            }
                        }
                        file = file.getParentFile();
                        file2 = file2.getParentFile();
                    }
                }
            }
        }
    }

    @TargetApi(19)
    public static void D(File file, File file2, boolean z) throws IOException {
        FileChannel channel;
        Throwable th;
        Throwable th2;
        ReadableByteChannel readableByteChannel;
        Object th3 = file;
        Object obj = file2;
        if (file2.exists() && file2.isDirectory()) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("Destination '");
            stringBuilder.append(obj);
            stringBuilder.append("' exists but is a directory");
            throw new IOException(stringBuilder.toString());
        }
        FileChannel channel2;
        FileOutputStream fileOutputStream;
        long size;
        long j;
        FileInputStream fileInputStream = new FileInputStream(th3);
        try {
            channel2 = fileInputStream.getChannel();
            try {
                fileOutputStream = new FileOutputStream(obj);
                try {
                    channel = fileOutputStream.getChannel();
                    try {
                        size = channel2.size();
                        j = 0;
                    } catch (Throwable th4) {
                        th2 = th4;
                        if (channel != null) {
                            channel.close();
                        }
                    }
                } catch (Throwable th42) {
                    th = th42;
                    try {
                    } catch (Throwable th422) {
                        th2 = th422;
                        fileOutputStream.close();
                    }
                }
            } catch (Throwable th4222) {
                th = th4222;
                try {
                } catch (Throwable th42222) {
                    th2 = th42222;
                    if (channel2 != null) {
                        channel2.close();
                    }
                }
            }
        } catch (Throwable th5) {
            th3 = th5;
            th.addSuppressed(readableByteChannel);
        } finally {
            th = th42222;
            try {
            } catch (Throwable th422222) {
                th2 = th422222;
                try {
                    fileInputStream.close();
                } catch (Throwable th4222222) {
                    th.addSuppressed(th4222222);
                }
            }
        }
        long j2;
        while (j2 < size) {
            long j3 = size - j2;
            readableByteChannel = channel2;
            j3 = channel.transferFrom(readableByteChannel, j2, j3 > 31457280 ? 31457280 : j3);
            if (j3 == j) {
                break;
            }
            j2 += j3;
        }
        if (channel != null) {
            channel.close();
        }
        fileOutputStream.close();
        channel2.close();
        fileInputStream.close();
        long length = file.length();
        long length2 = file2.length();
        if (length != length2) {
            StringBuilder stringBuilder2 = new StringBuilder();
            stringBuilder2.append("Failed to copy full contents from '");
            stringBuilder2.append(th3);
            stringBuilder2.append("' to '");
            stringBuilder2.append(obj);
            stringBuilder2.append("' Expected length: ");
            stringBuilder2.append(length);
            stringBuilder2.append(" Actual: ");
            stringBuilder2.append(length2);
            throw new IOException(stringBuilder2.toString());
        } else if (z) {
            obj.setLastModified(file.lastModified());
        }
    }

    public static boolean E() {
        if (!M()) {
            boolean z;
            Context context = b.a;
            synchronized (b.class) {
                z = b.B;
            }
            if (z && q.a(true).g()) {
                return true;
            }
        }
        return false;
    }

    public static boolean F(long j) {
        return j == -1;
    }

    public static boolean G(Context context) {
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
            if (connectivityManager == null) {
                return false;
            }
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            if (activeNetworkInfo == null) {
                return false;
            }
            return activeNetworkInfo.isConnected();
        } catch (Exception unused) {
            return false;
        }
    }

    public static boolean H(com.ss.android.socialbase.downloader.e.a aVar) {
        if (aVar instanceof com.ss.android.socialbase.downloader.e.c) {
            int i = ((com.ss.android.socialbase.downloader.e.c) aVar).c;
            if (i == 412 || i == 416) {
                return true;
            }
        }
        return false;
    }

    public static boolean I(File file, File file2) throws com.ss.android.socialbase.downloader.e.a {
        String str = a;
        StringBuilder g = c.b.a.a.a.g("moveFile1: src:");
        g.append(file.getPath());
        String str2 = " dest:";
        g.append(str2);
        g.append(file2.getPath());
        Log.e(str, g.toString());
        boolean renameTo = file.renameTo(file2);
        if (!renameTo) {
            renameTo = w(file, file2);
            try {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("moveFile2: src:");
                stringBuilder.append(file.getPath());
                stringBuilder.append(str2);
                stringBuilder.append(file2.getPath());
                Log.e(str, stringBuilder.toString());
                file.delete();
            } catch (Throwable th) {
                th.printStackTrace();
            }
        }
        return renameTo;
    }

    public static long J(String str) throws com.ss.android.socialbase.downloader.e.a {
        try {
            return new StatFs(str).getAvailableBytes();
        } catch (IllegalArgumentException e) {
            throw new com.ss.android.socialbase.downloader.e.a(1050, e);
        } catch (Throwable e2) {
            com.ss.android.socialbase.downloader.e.a aVar = new com.ss.android.socialbase.downloader.e.a(1052, e2);
        }
    }

    public static String K(Context context) {
        String str = "Process";
        String str2 = b;
        if (!TextUtils.isEmpty(str2)) {
            return str2;
        }
        try {
            int myPid = Process.myPid();
            ActivityManager activityManager = (ActivityManager) context.getSystemService("activity");
            if (activityManager != null) {
                for (RunningAppProcessInfo runningAppProcessInfo : activityManager.getRunningAppProcesses()) {
                    if (runningAppProcessInfo.pid == myPid) {
                        if (c.j.a.d.b.g.a.b()) {
                            StringBuilder stringBuilder = new StringBuilder();
                            stringBuilder.append("processName = ");
                            stringBuilder.append(runningAppProcessInfo.processName);
                            c.j.a.d.b.g.a.d(str, stringBuilder.toString());
                        }
                        String str3 = runningAppProcessInfo.processName;
                        b = str3;
                        return str3;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String str4 = null;
        BufferedReader bufferedReader;
        try {
            StringBuilder stringBuilder2 = new StringBuilder();
            stringBuilder2.append("/proc/");
            stringBuilder2.append(Process.myPid());
            stringBuilder2.append("/cmdline");
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(stringBuilder2.toString()), "iso-8859-1"));
            try {
                StringBuilder stringBuilder3 = new StringBuilder();
                while (true) {
                    int read = bufferedReader.read();
                    if (read <= 0) {
                        break;
                    }
                    stringBuilder3.append((char) read);
                }
                if (c.j.a.d.b.g.a.b()) {
                    StringBuilder stringBuilder4 = new StringBuilder();
                    stringBuilder4.append("get processName = ");
                    stringBuilder4.append(stringBuilder3.toString());
                    c.j.a.d.b.g.a.d(str, stringBuilder4.toString());
                }
                str4 = stringBuilder3.toString();
                q(bufferedReader);
            } catch (Throwable unused) {
                q(bufferedReader);
                b = str4;
                return str4;
            }
        } catch (Throwable unused2) {
            bufferedReader = null;
            q(bufferedReader);
            b = str4;
            return str4;
        }
        b = str4;
        return str4;
    }

    public static void L(String str, String str2) {
        if (!TextUtils.isEmpty(str) && !TextUtils.isEmpty(str2)) {
            File file = new File(str, str2);
            if (file.exists()) {
                String str3 = a;
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("deleteFile: ");
                stringBuilder.append(str);
                stringBuilder.append("/");
                stringBuilder.append(str2);
                Log.e(str3, stringBuilder.toString());
                file.delete();
            }
        }
    }

    public static boolean M() {
        Boolean bool = i;
        if (bool != null) {
            return bool.booleanValue();
        }
        String K = K(b.a());
        if (K != null) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(b.a().getPackageName());
            stringBuilder.append(":downloader");
            if (K.equals(stringBuilder.toString())) {
                i = Boolean.TRUE;
                return i.booleanValue();
            }
        }
        i = Boolean.FALSE;
        return i.booleanValue();
    }

    public static boolean N(f fVar) {
        boolean z = true;
        if (i.s(8)) {
            if (!("chunked".equals(fVar.a("Transfer-Encoding")) || b(fVar) == -1)) {
                z = false;
            }
            return z;
        }
        if (b(fVar) != -1) {
            z = false;
        }
        return z;
    }

    public static long O(c cVar) {
        long j = -1;
        if (cVar == null) {
            return -1;
        }
        List<com.ss.android.socialbase.downloader.g.b> c = b.x().c(cVar.g0());
        long j2 = 0;
        if (cVar.R == 1) {
            j2 = cVar.I();
        } else if (c != null && c.size() > 1) {
            if (!c.isEmpty()) {
                long j3 = -1;
                for (com.ss.android.socialbase.downloader.g.b bVar : c) {
                    if (bVar != null) {
                        long q = bVar.q();
                        long j4 = bVar.d;
                        if ((q <= j4 || j4 == 0) && (j3 == -1 || j3 > bVar.q())) {
                            j3 = bVar.q();
                        }
                    }
                }
                j = j3;
            }
            if (j >= 0) {
                j2 = j;
            }
        }
        return j2;
    }

    public static String P(String str) {
        if (str != null) {
            try {
                if (str.length() != 0) {
                    MessageDigest instance = MessageDigest.getInstance("MD5");
                    instance.update(str.getBytes("UTF-8"));
                    return g(instance.digest());
                }
            } catch (Exception unused) {
            }
        }
        return null;
    }

    public static boolean Q() {
        return Looper.getMainLooper() == Looper.myLooper();
    }

    public static boolean R(String str, String str2) {
        return (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) ? false : new File(str, str2).exists();
    }

    public static g S(int i) {
        g gVar = g.MAIN;
        g gVar2 = g.SUB;
        if (i != 1) {
            gVar2 = g.NOTIFICATION;
            if (i != 2) {
                return gVar;
            }
        }
        return gVar2;
    }

    public static boolean T(Throwable th) {
        boolean z = false;
        if (th == null) {
            return false;
        }
        String Y = Y(th);
        if (!TextUtils.isEmpty(Y) && Y.contains("Precondition Failed")) {
            z = true;
        }
        return z;
    }

    public static String U(String str) {
        StringBuilder stringBuilder = new StringBuilder();
        int length = str.length();
        for (int i = 0; i < length; i++) {
            char charAt = str.charAt(i);
            if (charAt <= 31 || charAt >= 127) {
                stringBuilder.append(String.format("\\u%04x", new Object[]{Integer.valueOf(charAt)}));
            } else {
                stringBuilder.append(charAt);
            }
        }
        return stringBuilder.toString();
    }

    public static boolean V(Throwable th) {
        boolean z = false;
        if (th == null) {
            return false;
        }
        String Y = Y(th);
        if (!TextUtils.isEmpty(Y) && Y.contains("Requested Range Not Satisfiable")) {
            z = true;
        }
        return z;
    }

    public static boolean W(Throwable th) {
        boolean z = false;
        if (th == null) {
            return false;
        }
        String str = "ENOSPC";
        String message;
        if (th instanceof com.ss.android.socialbase.downloader.e.a) {
            com.ss.android.socialbase.downloader.e.a aVar = (com.ss.android.socialbase.downloader.e.a) th;
            int i = aVar.a;
            if (i == 1006) {
                return true;
            }
            Object obj = (i == 1023 || i == 1039 || i == 1040 || i == 1054 || i == 1064) ? 1 : null;
            if (obj != null) {
                message = aVar.getMessage();
                if (!TextUtils.isEmpty(message) && message.contains(str)) {
                    z = true;
                }
                return z;
            }
        } else if (th instanceof IOException) {
            message = Y(th);
            if (!TextUtils.isEmpty(message) && message.contains(str)) {
                z = true;
            }
        }
        return z;
    }

    public static boolean X(Throwable th) {
        if (!(th instanceof com.ss.android.socialbase.downloader.e.a)) {
            return false;
        }
        int i = ((com.ss.android.socialbase.downloader.e.a) th).a;
        if (i == 1055 || i == 1023 || i == 1041 || i == 1022 || i == 1048 || i == 1056 || i == 1057 || i == 1058 || i == 1059 || i == 1060 || i == 1061 || i == 1067 || i == 1049 || i == 1047 || i == 1051 || i == 1004 || i == 1011 || i == 1002 || i == 1013) {
            return true;
        }
        return false;
    }

    public static String Y(Throwable th) {
        if (th == null) {
            return "";
        }
        try {
            return th.toString();
        } catch (Throwable th2) {
            th2.printStackTrace();
            return "throwable getMsg error";
        }
    }

    public static double a(long j) {
        return ((double) j) / 1048576.0d;
    }

    public static long b(f fVar) {
        long j = -1;
        if (fVar == null) {
            return -1;
        }
        String A = A(fVar, "Content-Length");
        if (TextUtils.isEmpty(A) && i.s(1)) {
            String A2 = A(fVar, "Content-Range");
            if (!TextUtils.isEmpty(A2)) {
                try {
                    Matcher matcher = Pattern.compile("bytes (\\d+)-(\\d+)/\\d+").matcher(A2);
                    if (matcher.find()) {
                        j = (Long.parseLong(matcher.group(2)) - Long.parseLong(matcher.group(1))) + 1;
                    }
                } catch (Exception e) {
                    A = a;
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append("parse content-length from content-range failed ");
                    stringBuilder.append(e);
                    c.j.a.d.b.g.a.f(A, stringBuilder.toString());
                }
            }
        } else {
            try {
                j = Long.parseLong(A);
            } catch (NumberFormatException unused) {
            }
        }
        return j;
    }

    public static long c(String str) {
        long j = -1;
        if (str == null) {
            return -1;
        }
        String[] split = str.split("/");
        if (split.length >= 2) {
            try {
                j = Long.parseLong(split[1]);
                return j;
            } catch (NumberFormatException unused) {
                String str2 = a;
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("parse instance length failed with ");
                stringBuilder.append(str);
                c.j.a.d.b.g.a.f(str2, stringBuilder.toString());
            }
        }
        return j;
    }

    public static d d(String str, String str2, int i) throws com.ss.android.socialbase.downloader.e.a {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
            throw new com.ss.android.socialbase.downloader.e.a(1021, new IOException("path must be not empty"));
        }
        File file = new File(str, str2);
        if (file.exists() && file.isDirectory()) {
            throw new com.ss.android.socialbase.downloader.e.a(1035, new IOException(String.format("path is :%s, path is directory:%B:", new Object[]{str, Boolean.valueOf(file.isDirectory())})));
        }
        if (!file.exists()) {
            try {
                File file2 = new File(str);
                if (!(file2.exists() && file2.isDirectory())) {
                    if (file2.exists()) {
                        file2.delete();
                        throw new com.ss.android.socialbase.downloader.e.a(1031, "download savePath is not directory");
                    } else if (!file2.mkdirs()) {
                        throw new com.ss.android.socialbase.downloader.e.a(1030, "download savePath directory can not created");
                    }
                }
                file.createNewFile();
            } catch (IOException e) {
                throw new com.ss.android.socialbase.downloader.e.a(1036, e);
            }
        }
        return new d(file, i);
    }

    /* JADX WARNING: Removed duplicated region for block: B:21:? A:{SYNTHETIC, RETURN} */
    /* JADX WARNING: Removed duplicated region for block: B:19:0x004b  */
    public static java.lang.String e(c.j.a.d.b.i.f r3, java.lang.String r4) {
        /*
        r0 = "Content-Disposition";
        r3 = r3.a(r0);
        if (r3 != 0) goto L_0x0009;
    L_0x0008:
        goto L_0x0044;
    L_0x0009:
        r0 = f;	 Catch:{ Exception -> 0x0044 }
        if (r0 != 0) goto L_0x0015;
    L_0x000d:
        r0 = "attachment;\\s*filename\\s*=\\s*\"([^\"]*)\"";
        r0 = java.util.regex.Pattern.compile(r0);	 Catch:{ Exception -> 0x0044 }
        f = r0;	 Catch:{ Exception -> 0x0044 }
    L_0x0015:
        r0 = f;	 Catch:{ Exception -> 0x0044 }
        r0 = r0.matcher(r3);	 Catch:{ Exception -> 0x0044 }
        r1 = r0.find();	 Catch:{ Exception -> 0x0044 }
        r2 = 1;
        if (r1 == 0) goto L_0x0027;
    L_0x0022:
        r3 = r0.group(r2);	 Catch:{ Exception -> 0x0044 }
        goto L_0x0045;
    L_0x0027:
        r0 = g;	 Catch:{ Exception -> 0x0044 }
        if (r0 != 0) goto L_0x0033;
    L_0x002b:
        r0 = "attachment;\\s*filename\\s*=\\s*(.*)";
        r0 = java.util.regex.Pattern.compile(r0);	 Catch:{ Exception -> 0x0044 }
        g = r0;	 Catch:{ Exception -> 0x0044 }
    L_0x0033:
        r0 = g;	 Catch:{ Exception -> 0x0044 }
        r3 = r0.matcher(r3);	 Catch:{ Exception -> 0x0044 }
        r0 = r3.find();	 Catch:{ Exception -> 0x0044 }
        if (r0 == 0) goto L_0x0044;
    L_0x003f:
        r3 = r3.group(r2);	 Catch:{ Exception -> 0x0044 }
        goto L_0x0045;
    L_0x0044:
        r3 = 0;
    L_0x0045:
        r0 = android.text.TextUtils.isEmpty(r3);
        if (r0 == 0) goto L_0x004f;
    L_0x004b:
        r3 = P(r4);
    L_0x004f:
        return r3;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.j.a.d.b.n.a.e(c.j.a.d.b.i.f, java.lang.String):java.lang.String");
    }

    public static String f(String str, int i) {
        if (i == 0) {
            return "";
        }
        if (!TextUtils.isEmpty(str) && str.length() > i) {
            str = str.substring(0, i);
        }
        return str;
    }

    public static String g(byte[] bArr) {
        Objects.requireNonNull(bArr, "bytes is null");
        int length = bArr.length;
        if (0 + length <= bArr.length) {
            int i = length * 2;
            char[] cArr = new char[i];
            int i2 = 0;
            for (int i3 = 0; i3 < length; i3++) {
                int i4 = bArr[i3 + 0] & ISdkLite.REGION_UNSET;
                int i5 = i2 + 1;
                char[] cArr2 = e;
                cArr[i2] = cArr2[i4 >> 4];
                i2 = i5 + 1;
                cArr[i5] = cArr2[i4 & 15];
            }
            return new String(cArr, 0, i);
        }
        throw new IndexOutOfBoundsException();
    }

    public static <K> HashMap<Integer, K> h(SparseArray<K> sparseArray) {
        if (sparseArray == null) {
            return null;
        }
        HashMap hashMap = new HashMap();
        int size = sparseArray.size();
        for (int i = 0; i < size; i++) {
            int keyAt = sparseArray.keyAt(i);
            hashMap.put(Integer.valueOf(keyAt), sparseArray.valueAt(i));
        }
        return hashMap;
    }

    public static List<com.ss.android.socialbase.downloader.g.b> i(List<com.ss.android.socialbase.downloader.g.b> list) {
        SparseArray sparseArray = new SparseArray();
        SparseArray sparseArray2 = new SparseArray();
        for (com.ss.android.socialbase.downloader.g.b bVar : list) {
            if (bVar != null) {
                if (bVar.h()) {
                    sparseArray.put(bVar.f, bVar);
                    List<com.ss.android.socialbase.downloader.g.b> list2 = (List) sparseArray2.get(bVar.f);
                    if (list2 != null) {
                        for (com.ss.android.socialbase.downloader.g.b c : list2) {
                            c.c(bVar);
                        }
                        bVar.i = list2;
                    }
                } else {
                    com.ss.android.socialbase.downloader.g.b bVar2 = (com.ss.android.socialbase.downloader.g.b) sparseArray.get(bVar.e());
                    if (bVar2 != null) {
                        List list3 = bVar2.i;
                        if (list3 == null) {
                            list3 = new ArrayList();
                            bVar2.i = list3;
                        }
                        bVar.c(bVar2);
                        list3.add(bVar);
                    } else {
                        List list4 = (List) sparseArray2.get(bVar.e());
                        if (list4 == null) {
                            list4 = new ArrayList();
                            sparseArray2.put(bVar.e(), list4);
                        }
                        list4.add(bVar);
                    }
                }
            }
        }
        ArrayList arrayList = new ArrayList();
        for (int i = 0; i < sparseArray.size(); i++) {
            arrayList.add(sparseArray.get(sparseArray.keyAt(i)));
        }
        return arrayList.isEmpty() ? list : arrayList;
    }

    public static List<e> j(List<e> list, String str, long j, long j2) {
        ArrayList arrayList = new ArrayList();
        if (list != null && list.size() > 0) {
            for (e eVar : list) {
                if (eVar != null) {
                    arrayList.add(eVar);
                }
            }
        }
        if (!TextUtils.isEmpty(str)) {
            arrayList.add(new e("If-Match", str));
        }
        arrayList.add(new e("Accept-Encoding", "identity"));
        String str2 = a;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("range CurrentOffset:");
        stringBuilder.append(j);
        stringBuilder.append(" EndOffset:");
        stringBuilder.append(j2);
        c.j.a.d.b.g.a.d(str2, stringBuilder.toString());
        if (j2 == 0) {
            str2 = String.format("bytes=%s-", new Object[]{String.valueOf(j)});
        } else {
            str2 = String.format("bytes=%s-%s", new Object[]{String.valueOf(j), String.valueOf(j2)});
        }
        arrayList.add(new e("Range", str2));
        return arrayList;
    }

    public static void k(int i, boolean z, com.ss.android.socialbase.downloader.e.a aVar) {
        synchronized (c) {
            List<l> list = (List) d.get(i);
            if (list != null) {
                for (l lVar : list) {
                    if (lVar != null) {
                        if (z) {
                            lVar.a();
                        } else {
                            lVar.a(aVar);
                        }
                    }
                }
            }
            String str = a;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("handleTempSaveCallback id:");
            stringBuilder.append(i);
            c.j.a.d.b.g.a.d(str, stringBuilder.toString());
            c.remove(i);
        }
    }

    public static <K> void l(SparseArray<K> sparseArray, Map<Integer, K> map) {
        if (map != null && sparseArray != null) {
            for (Integer num : map.keySet()) {
                if (num != null) {
                    sparseArray.put(num.intValue(), map.get(num));
                }
            }
        }
    }

    public static void m(c cVar, String str) throws com.ss.android.socialbase.downloader.e.a {
        if (cVar != null && !TextUtils.isEmpty(str) && !str.equals(cVar.b)) {
            File file = new File(cVar.e, str);
            File file2 = new File(cVar.e, cVar.b);
            String str2 = a;
            StringBuilder g = c.b.a.a.a.g("copyFileFromExistFileWithSameName: existFile:");
            g.append(file.getPath());
            g.append(" targetFile:");
            g.append(file2.getPath());
            Log.e(str2, g.toString());
            if (file2.exists() && !file2.delete()) {
                throw new com.ss.android.socialbase.downloader.e.a(1037, "targetPath file exists but can't delete");
            } else if ((w(file, file2) ^ 1) != 0) {
                r3 = new Object[4];
                String str3 = cVar.e;
                r3[0] = str3;
                r3[1] = str;
                r3[2] = str3;
                r3[3] = cVar.b;
                throw new com.ss.android.socialbase.downloader.e.a((int) StatusCode.API_CLIENT_EXPIRED, String.format("Can't copy the exist file(%s/%s) to the target file(%s/%s)", r3));
            }
        }
    }

    public static void n(c cVar, boolean z) {
        if (cVar != null) {
            if (z) {
                try {
                    L(cVar.e, cVar.b);
                } catch (Throwable th) {
                    th.printStackTrace();
                }
            }
            L(cVar.k0(), cVar.l0());
            if (cVar.X()) {
                C(cVar);
            }
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:134:0x01a7  */
    /* JADX WARNING: Removed duplicated region for block: B:50:0x009c  */
    /* JADX WARNING: Removed duplicated region for block: B:136:0x01b3  */
    /* JADX WARNING: Removed duplicated region for block: B:42:0x0082  */
    /* JADX WARNING: Removed duplicated region for block: B:142:0x01d7  */
    /* JADX WARNING: Removed duplicated region for block: B:22:0x003e  */
    /* JADX WARNING: Missing block: B:54:0x00af, code skipped:
            if (r3.contains("Exception in connect") != false) goto L_0x00b3;
     */
    public static void o(java.lang.Throwable r7, java.lang.String r8) throws com.ss.android.socialbase.downloader.e.a {
        /*
        r0 = android.text.TextUtils.isEmpty(r8);
        if (r0 != 0) goto L_0x0008;
    L_0x0006:
        r0 = r8;
        goto L_0x000a;
    L_0x0008:
        r0 = "";
    L_0x000a:
        r1 = r7 instanceof com.ss.android.socialbase.downloader.e.a;
        if (r1 != 0) goto L_0x01eb;
    L_0x000e:
        r1 = r7 instanceof javax.net.ssl.SSLHandshakeException;
        r2 = 1011; // 0x3f3 float:1.417E-42 double:4.995E-321;
        if (r1 != 0) goto L_0x01e1;
    L_0x0014:
        r1 = 1;
        if (r7 != 0) goto L_0x0018;
    L_0x0017:
        goto L_0x0037;
    L_0x0018:
        r3 = Y(r7);
        r4 = r7 instanceof java.net.SocketTimeoutException;
        if (r4 != 0) goto L_0x0039;
    L_0x0020:
        r4 = android.text.TextUtils.isEmpty(r3);
        if (r4 != 0) goto L_0x0037;
    L_0x0026:
        r4 = "time out";
        r4 = r3.contains(r4);
        if (r4 != 0) goto L_0x0039;
    L_0x002e:
        r4 = "Time-out";
        r3 = r3.contains(r4);
        if (r3 == 0) goto L_0x0037;
    L_0x0036:
        goto L_0x0039;
    L_0x0037:
        r3 = 0;
        goto L_0x003a;
    L_0x0039:
        r3 = 1;
    L_0x003a:
        r4 = 1048; // 0x418 float:1.469E-42 double:5.18E-321;
        if (r3 != 0) goto L_0x01d7;
    L_0x003e:
        r3 = T(r7);
        r5 = 1004; // 0x3ec float:1.407E-42 double:4.96E-321;
        if (r3 != 0) goto L_0x01cb;
    L_0x0046:
        r3 = V(r7);
        if (r3 != 0) goto L_0x01bf;
    L_0x004c:
        if (r7 != 0) goto L_0x004f;
    L_0x004e:
        goto L_0x007f;
    L_0x004f:
        r3 = Y(r7);
        r5 = r7 instanceof com.ss.android.socialbase.downloader.e.c;
        if (r5 == 0) goto L_0x006f;
    L_0x0057:
        r5 = r7;
        r5 = (com.ss.android.socialbase.downloader.e.c) r5;
        r5 = r5.c;
        r6 = 403; // 0x193 float:5.65E-43 double:1.99E-321;
        if (r5 == r6) goto L_0x007d;
    L_0x0060:
        r5 = android.text.TextUtils.isEmpty(r3);
        if (r5 != 0) goto L_0x006f;
    L_0x0066:
        r5 = "403";
        r5 = r3.contains(r5);
        if (r5 == 0) goto L_0x006f;
    L_0x006e:
        goto L_0x007d;
    L_0x006f:
        r5 = android.text.TextUtils.isEmpty(r3);
        if (r5 != 0) goto L_0x007f;
    L_0x0075:
        r5 = "Forbidden";
        r3 = r3.contains(r5);
        if (r3 == 0) goto L_0x007f;
    L_0x007d:
        r3 = 1;
        goto L_0x0080;
    L_0x007f:
        r3 = 0;
    L_0x0080:
        if (r3 != 0) goto L_0x01b3;
    L_0x0082:
        if (r7 != 0) goto L_0x0085;
    L_0x0084:
        goto L_0x0099;
    L_0x0085:
        r3 = Y(r7);
        r5 = android.text.TextUtils.isEmpty(r3);
        if (r5 != 0) goto L_0x0099;
    L_0x008f:
        r5 = "network not available";
        r3 = r3.contains(r5);
        if (r3 == 0) goto L_0x0099;
    L_0x0097:
        r3 = 1;
        goto L_0x009a;
    L_0x0099:
        r3 = 0;
    L_0x009a:
        if (r3 != 0) goto L_0x01a7;
    L_0x009c:
        if (r7 != 0) goto L_0x009f;
    L_0x009e:
        goto L_0x00b2;
    L_0x009f:
        r3 = Y(r7);
        r5 = android.text.TextUtils.isEmpty(r3);
        if (r5 != 0) goto L_0x00b2;
    L_0x00a9:
        r5 = "Exception in connect";
        r3 = r3.contains(r5);
        if (r3 == 0) goto L_0x00b2;
    L_0x00b1:
        goto L_0x00b3;
    L_0x00b2:
        r1 = 0;
    L_0x00b3:
        r3 = 1041; // 0x411 float:1.459E-42 double:5.143E-321;
        if (r1 != 0) goto L_0x019d;
    L_0x00b7:
        r1 = r7 instanceof java.io.IOException;
        if (r1 == 0) goto L_0x0191;
    L_0x00bb:
        r7 = (java.io.IOException) r7;
        r8 = B(r7, r8);
        r0 = r7 instanceof java.net.ConnectException;
        if (r0 != 0) goto L_0x018b;
    L_0x00c5:
        r0 = r7 instanceof java.net.UnknownHostException;
        if (r0 != 0) goto L_0x0183;
    L_0x00c9:
        r0 = r7 instanceof java.net.NoRouteToHostException;
        if (r0 != 0) goto L_0x017b;
    L_0x00cd:
        r0 = r7 instanceof java.net.UnknownServiceException;
        if (r0 != 0) goto L_0x0173;
    L_0x00d1:
        r0 = r7 instanceof java.net.PortUnreachableException;
        if (r0 != 0) goto L_0x016b;
    L_0x00d5:
        r0 = r7 instanceof java.net.SocketTimeoutException;
        if (r0 != 0) goto L_0x0165;
    L_0x00d9:
        r0 = r7 instanceof java.net.SocketException;
        if (r0 != 0) goto L_0x015d;
    L_0x00dd:
        r0 = r7 instanceof java.net.HttpRetryException;
        if (r0 != 0) goto L_0x0155;
    L_0x00e1:
        r0 = r7 instanceof java.net.ProtocolException;
        if (r0 != 0) goto L_0x014d;
    L_0x00e5:
        r0 = r7 instanceof java.net.MalformedURLException;
        if (r0 != 0) goto L_0x0145;
    L_0x00e9:
        r0 = r7 instanceof java.io.FileNotFoundException;
        if (r0 != 0) goto L_0x013d;
    L_0x00ed:
        r0 = r7 instanceof java.io.InterruptedIOException;
        if (r0 != 0) goto L_0x0135;
    L_0x00f1:
        r0 = r7 instanceof java.io.UnsupportedEncodingException;
        if (r0 != 0) goto L_0x012d;
    L_0x00f5:
        r0 = r7 instanceof java.io.EOFException;
        if (r0 != 0) goto L_0x0125;
    L_0x00f9:
        r0 = r7 instanceof f.f0.i.u;
        if (r0 != 0) goto L_0x011d;
    L_0x00fd:
        r0 = r7 instanceof javax.net.ssl.SSLException;
        if (r0 != 0) goto L_0x0117;
    L_0x0101:
        r7 = W(r7);
        if (r7 == 0) goto L_0x010f;
    L_0x0107:
        r7 = new com.ss.android.socialbase.downloader.e.a;
        r0 = 1006; // 0x3ee float:1.41E-42 double:4.97E-321;
        r7.m4677init(r0, r8);
        throw r7;
    L_0x010f:
        r7 = new com.ss.android.socialbase.downloader.e.a;
        r0 = 1023; // 0x3ff float:1.434E-42 double:5.054E-321;
        r7.m4677init(r0, r8);
        throw r7;
    L_0x0117:
        r7 = new com.ss.android.socialbase.downloader.e.a;
        r7.m4677init(r2, r8);
        throw r7;
    L_0x011d:
        r7 = new com.ss.android.socialbase.downloader.e.a;
        r0 = 1067; // 0x42b float:1.495E-42 double:5.27E-321;
        r7.m4677init(r0, r8);
        throw r7;
    L_0x0125:
        r7 = new com.ss.android.socialbase.downloader.e.a;
        r0 = 1066; // 0x42a float:1.494E-42 double:5.267E-321;
        r7.m4677init(r0, r8);
        throw r7;
    L_0x012d:
        r7 = new com.ss.android.socialbase.downloader.e.a;
        r0 = 1065; // 0x429 float:1.492E-42 double:5.26E-321;
        r7.m4677init(r0, r8);
        throw r7;
    L_0x0135:
        r7 = new com.ss.android.socialbase.downloader.e.a;
        r0 = 1064; // 0x428 float:1.491E-42 double:5.257E-321;
        r7.m4677init(r0, r8);
        throw r7;
    L_0x013d:
        r7 = new com.ss.android.socialbase.downloader.e.a;
        r0 = 1063; // 0x427 float:1.49E-42 double:5.25E-321;
        r7.m4677init(r0, r8);
        throw r7;
    L_0x0145:
        r7 = new com.ss.android.socialbase.downloader.e.a;
        r0 = 1062; // 0x426 float:1.488E-42 double:5.247E-321;
        r7.m4677init(r0, r8);
        throw r7;
    L_0x014d:
        r7 = new com.ss.android.socialbase.downloader.e.a;
        r0 = 1061; // 0x425 float:1.487E-42 double:5.24E-321;
        r7.m4677init(r0, r8);
        throw r7;
    L_0x0155:
        r7 = new com.ss.android.socialbase.downloader.e.a;
        r0 = 1060; // 0x424 float:1.485E-42 double:5.237E-321;
        r7.m4677init(r0, r8);
        throw r7;
    L_0x015d:
        r7 = new com.ss.android.socialbase.downloader.e.a;
        r0 = 1059; // 0x423 float:1.484E-42 double:5.23E-321;
        r7.m4677init(r0, r8);
        throw r7;
    L_0x0165:
        r7 = new com.ss.android.socialbase.downloader.e.a;
        r7.m4677init(r4, r8);
        throw r7;
    L_0x016b:
        r7 = new com.ss.android.socialbase.downloader.e.a;
        r0 = 1058; // 0x422 float:1.483E-42 double:5.227E-321;
        r7.m4677init(r0, r8);
        throw r7;
    L_0x0173:
        r7 = new com.ss.android.socialbase.downloader.e.a;
        r0 = 1057; // 0x421 float:1.481E-42 double:5.22E-321;
        r7.m4677init(r0, r8);
        throw r7;
    L_0x017b:
        r7 = new com.ss.android.socialbase.downloader.e.a;
        r0 = 1056; // 0x420 float:1.48E-42 double:5.217E-321;
        r7.m4677init(r0, r8);
        throw r7;
    L_0x0183:
        r7 = new com.ss.android.socialbase.downloader.e.a;
        r0 = 1055; // 0x41f float:1.478E-42 double:5.21E-321;
        r7.m4677init(r0, r8);
        throw r7;
    L_0x018b:
        r7 = new com.ss.android.socialbase.downloader.e.a;
        r7.m4677init(r3, r8);
        throw r7;
    L_0x0191:
        r8 = new com.ss.android.socialbase.downloader.e.a;
        r1 = 1000; // 0x3e8 float:1.401E-42 double:4.94E-321;
        r7 = B(r7, r0);
        r8.m4677init(r1, r7);
        throw r8;
    L_0x019d:
        r8 = new com.ss.android.socialbase.downloader.e.a;
        r7 = B(r7, r0);
        r8.m4677init(r3, r7);
        throw r8;
    L_0x01a7:
        r8 = new com.ss.android.socialbase.downloader.e.a;
        r1 = 1049; // 0x419 float:1.47E-42 double:5.183E-321;
        r7 = B(r7, r0);
        r8.m4677init(r1, r7);
        throw r8;
    L_0x01b3:
        r8 = new com.ss.android.socialbase.downloader.e.a;
        r1 = 1047; // 0x417 float:1.467E-42 double:5.173E-321;
        r7 = B(r7, r0);
        r8.m4677init(r1, r7);
        throw r8;
    L_0x01bf:
        r8 = new com.ss.android.socialbase.downloader.e.c;
        r1 = 416; // 0x1a0 float:5.83E-43 double:2.055E-321;
        r7 = B(r7, r0);
        r8.m7696init(r5, r1, r7);
        throw r8;
    L_0x01cb:
        r8 = new com.ss.android.socialbase.downloader.e.c;
        r1 = 412; // 0x19c float:5.77E-43 double:2.036E-321;
        r7 = B(r7, r0);
        r8.m7696init(r5, r1, r7);
        throw r8;
    L_0x01d7:
        r8 = new com.ss.android.socialbase.downloader.e.a;
        r7 = B(r7, r0);
        r8.m4677init(r4, r7);
        throw r8;
    L_0x01e1:
        r8 = new com.ss.android.socialbase.downloader.e.a;
        r7 = B(r7, r0);
        r8.m4677init(r2, r7);
        throw r8;
    L_0x01eb:
        r7 = (com.ss.android.socialbase.downloader.e.a) r7;
        r8 = "-";
        r8 = c.b.a.a.a.i(r0, r8);
        r0 = r7.b;
        r8.append(r0);
        r8 = r8.toString();
        r7.b = r8;
        throw r7;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.j.a.d.b.n.a.o(java.lang.Throwable, java.lang.String):void");
    }

    public static void p(List<e> list, c cVar) {
        long j = cVar.u0;
        if (j > 0) {
            list.add(new e("extra_throttle_net_speed", String.valueOf(j)));
        }
    }

    public static void q(Closeable... closeableArr) {
        for (Closeable closeable : closeableArr) {
            if (closeable != null) {
                try {
                    closeable.close();
                } catch (Throwable th) {
                    th.printStackTrace();
                }
            }
        }
    }

    public static boolean r() {
        Boolean bool = h;
        if (bool != null) {
            return bool.booleanValue();
        }
        String K = K(b.a());
        if (K == null || !K.contains(":")) {
            boolean z = K != null && K.equals(b.a().getPackageName());
            h = Boolean.valueOf(z);
        } else {
            h = Boolean.FALSE;
        }
        return h.booleanValue();
    }

    public static boolean s(int i) {
        return i == 0 || i == 2;
    }

    public static boolean t(Context context) {
        boolean z = false;
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
            if (connectivityManager == null) {
                return false;
            }
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            if (activeNetworkInfo != null) {
                if (activeNetworkInfo.isAvailable()) {
                    if (1 == activeNetworkInfo.getType()) {
                        z = true;
                    }
                }
            }
            return z;
        } catch (Exception unused) {
        }
    }

    public static boolean u(Context context, String str) {
        return (context == null || TextUtils.isEmpty(str) || context.checkCallingOrSelfPermission(str) != 0) ? false : true;
    }

    public static boolean v(c cVar, boolean z, String str) {
        if (!(z || TextUtils.isEmpty(cVar.e) || TextUtils.isEmpty(cVar.b))) {
            try {
                if (new File(cVar.e, cVar.b).exists() && s(c.j.a.e.b.b(str, new File(cVar.e, cVar.b)))) {
                    return true;
                }
            } catch (OutOfMemoryError e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static boolean w(File file, File file2) throws com.ss.android.socialbase.downloader.e.a {
        try {
            if (file.exists() && !file.isDirectory()) {
                if (!file.getCanonicalPath().equals(file2.getCanonicalPath())) {
                    File parentFile = file2.getParentFile();
                    String str = "Destination '";
                    if (parentFile != null) {
                        if (!parentFile.mkdirs()) {
                            if (!parentFile.isDirectory()) {
                                StringBuilder stringBuilder = new StringBuilder();
                                stringBuilder.append(str);
                                stringBuilder.append(parentFile);
                                stringBuilder.append("' directory cannot be created");
                                throw new com.ss.android.socialbase.downloader.e.a(1053, stringBuilder.toString());
                            }
                        }
                    }
                    String str2 = a;
                    StringBuilder stringBuilder2 = new StringBuilder();
                    stringBuilder2.append("copyFile: srcFile:");
                    stringBuilder2.append(file.getPath());
                    stringBuilder2.append(" destFile:");
                    stringBuilder2.append(file2.getPath());
                    Log.e(str2, stringBuilder2.toString());
                    if (file2.exists() && !file2.canWrite()) {
                        if (!file2.delete()) {
                            StringBuilder stringBuilder3 = new StringBuilder();
                            stringBuilder3.append(str);
                            stringBuilder3.append(file2);
                            stringBuilder3.append("' exists but is read-only and delete failed");
                            throw new IOException(stringBuilder3.toString());
                        }
                    }
                    D(file, file2, true);
                    return true;
                }
            }
            return false;
        } catch (com.ss.android.socialbase.downloader.e.a e) {
            throw e;
        } catch (Throwable th) {
            o(th, "CopyFile");
        }
    }

    public static boolean x(List<e> list, List<e> list2) {
        if (list == list2) {
            return true;
        }
        if (list == null || list2 == null || list.size() != list2.size()) {
            return false;
        }
        return new HashSet(list).equals(new HashSet(list2));
    }

    public static long y(List<com.ss.android.socialbase.downloader.g.b> list) {
        long j = 0;
        for (com.ss.android.socialbase.downloader.g.b r : list) {
            j += r.r();
        }
        return j;
    }

    public static String z(int i) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("ttmd5 check code = ");
        stringBuilder.append(i);
        stringBuilder.append(", ");
        String stringBuilder2 = stringBuilder.toString();
        if (i == 99) {
            return c.b.a.a.a.D(stringBuilder2, "unknown error");
        }
        switch (i) {
            case 0:
                return c.b.a.a.a.D(stringBuilder2, "md5 match");
            case 1:
                return c.b.a.a.a.D(stringBuilder2, "md5 not match");
            case 2:
                return c.b.a.a.a.D(stringBuilder2, "md5 empty");
            case 3:
                return c.b.a.a.a.D(stringBuilder2, "ttmd5 version not support");
            case 4:
                return c.b.a.a.a.D(stringBuilder2, "ttmd5 tag parser error");
            case 5:
                return c.b.a.a.a.D(stringBuilder2, "file not exist");
            case 6:
                return c.b.a.a.a.D(stringBuilder2, "get file md5 error");
            default:
                return stringBuilder2;
        }
    }
}
